package com.tsingteng.cosfun.ui.setting;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.base.BasePresenterFragment;
import com.tsingteng.cosfun.bean.UserBean;
import com.tsingteng.cosfun.utils.LogonUtils;
import com.tsingteng.cosfun.utils.MobileUtils;
import com.tsingteng.cosfun.widget.MyRecycleView;
import com.tsingteng.cosfun.widget.TitleView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/23
 * qq(872179966)
 */

public class MobileBindFragment extends BasePresenterFragment<MobileBindPresenter, MobileBindContract.IMobileBindView> implements MobileBindContract.IMobileBindView, TitleView.OnTitleClickListener {
    @BindView(R.id.title)
    TitleView title;
    @BindView(R.id.tv_hint)
    TextView tvHint;
    @BindView(R.id.et_mobile)
    EditText etMobile;
    @BindView(R.id.et_verify_code)
    EditText etVerifyCode;
    @BindView(R.id.tv_bt_verify_code)
    TextView tvBtVerifyCode;
    @BindView(R.id.tv_bind_mobile)
    TextView tvBindMobile;
    @BindView(R.id.ll_bt_bind)
    LinearLayout llBtBind;
    private String mobile;
    private boolean isBind = false;


    public static MobileBindFragment newInstance(String mobile) {
        MobileBindFragment fragment = new MobileBindFragment();
        Bundle bundle = new Bundle();
        bundle.putString("mobile",mobile);
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    protected void getBundle(Bundle bundle) {
        mobile = bundle.getString("mobile");
    }

    @Override
    public void showMobileBindResult(String status) {
        showToast("操作成功!");
        getActivity().finish();
    }

    @Override
    public void showVerifyCodeResult(UserBean data) {
        if (data.getStatus() == 1) {
            LogonUtils.runTime(tvBtVerifyCode);
            showToast("验证码发送成功!");
        }
    }

    @Override
    protected void initData(View rootView) {
        title.setOnTitleClickListener(this);
        if (TextUtils.isEmpty(mobile)) {
            isBind = true;
            title.setTitle("绑定手机号");
            tvBindMobile.setText("确认绑定");
            tvHint.setVisibility(View.GONE);
            etMobile.setVisibility(View.VISIBLE);
        } else {
            isBind = false;
            title.setTitle("解绑手机号");
            tvBindMobile.setText("确认解绑");
            tvHint.setText("请输入绑定手机"+ MobileUtils.HindPhoneNumber(mobile)+"收到的验证码");
            tvHint.setVisibility(View.VISIBLE);
            etMobile.setVisibility(View.GONE);
        }
        title.setIvLeft(R.drawable.back_big_icon);
    }

    @Override
    protected MobileBindPresenter createPresenter() {
        return new MobileBindPresenter();
    }

    @Override
    protected View initHeader() {
        return null;
    }

    @Override
    protected MyRecycleView createRecycleView() {
        return null;
    }

    @Override
    protected BaseQuickAdapter createAdapter() {
        return null;
    }

    @Override
    protected void refresh() {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    @Override
    public void onLoadMoreRequested() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mobile_bind;
    }


    @OnClick({R.id.tv_bt_verify_code, R.id.ll_bt_bind})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_bt_verify_code:
                if (isBind) {
                    if (TextUtils.isEmpty(etMobile.getText().toString().trim())) {
                        showToast("手机号不能为空!");
                        return;
                    }
                    mobile = etMobile.getText().toString().trim();
                    mPresenter.getVerifyCode(mobile,"3");
                }else {
                    mPresenter.getVerifyCode(mobile,"4");
                }

                break;
            case R.id.ll_bt_bind:
                if (TextUtils.isEmpty(etVerifyCode.getText().toString().trim())) {
                    showToast("验证码不能为空!");
                    return;
                }
                if (isBind) {
                    if (TextUtils.isEmpty(etMobile.getText().toString().trim())) {
                        showToast("手机号不能为空!");
                        return;
                    }
                    mPresenter.getMobileBind(etMobile.getText().toString().trim(),etVerifyCode.getText().toString().trim(),"1");
                }else {
                    mPresenter.getMobileBind(mobile,etVerifyCode.getText().toString().trim(),"2");
                }
                break;
        }
    }

    @Override
    public void onLeft() {
        getActivity().finish();
    }

    @Override
    public void onRight() {

    }

    @Override
    public void onRight2() {

    }
}
